﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace HasuSLUI
{
    //http://weblogs.asp.net/fredriknormen/archive/2010/01/06/silverlight-about-validation-when-binding-to-custom-forms.aspx
    public class MetaControl : StackPanel,IDisposable
    {
        public FrameworkElement DataControl { get; set; }
        
        public TextBlock LabelControl { get; set; }

        public DescriptionViewer DescrViewer = new DescriptionViewer();

        public string MetaDataName { get; set; }

        public MetaControl(TextBlock label, Control control)
            :base()
        {
            this.Children.Add(label);
            this.Children.Add(control);
            this.Children.Add(DescrViewer);
            LabelControl = label;
            DataControl = control;
        }
        public MetaControl(TextBlock label, TextBlock control)
            : base()
        {
            this.Children.Add(label);
            this.Children.Add(control);
            this.Children.Add(DescrViewer);

            LabelControl = label;
            DataControl = control;
        }

        public MetaControl(Control control)
            : base()
        {
            this.Children.Add(control);
            this.Children.Add(DescrViewer);
            DataControl = control;
        }


        public MetaControl()
        {
            this.SizeChanged += new SizeChangedEventHandler(MetaControl_SizeChanged);
            Orientation = Orientation.Horizontal;
        }

        public void AddControls(TextBlock label, Control control)
        {
            this.Children.Add(label);
            this.Children.Add(control);
            this.Children.Add(DescrViewer);
            LabelControl = label;
            DataControl = control;
        }

        public void AddControls(TextBlock label, TextBlock control)
        {
            this.Children.Add(label);
            this.Children.Add(control);
            LabelControl = label;
            DataControl = control;
        }

        void MetaControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (DataControl != null && DataControl is IFullWidth)
            {
                DataControl.Width = this.Width - (LabelControl == null ? 0 : LabelControl.Width);
            }
        }

        public new double Width
        {
            get { return base.Width; }
            set
            {
                base.Width = value;
                MetaControl_SizeChanged(this, null);
            }
        }

        public new object Tag 
        {
            get { return base.Tag ; }
            set 
            {
                base.Tag = value;
                if (value !=null && value is IClientValidate)
                {
                    (value as IClientValidate).ClientValidate = new ClientValidateDelegate(clientValidate);
                }
                if (value != null && value is Metadata)
                {
                    DescrViewer.Description= (value as Metadata).Description;
                }
            }
        }

        private void clientValidate(bool valid, string errorMessage)
        {
            if (valid)
            {
                if (DataControl != null && (DataControl is Control))
                {
                    (DataControl as Control).ClearErrorState();
                }
            }
            else
            {
                if (DataControl != null && (DataControl is Control) )
                {
                    (DataControl as Control).SetErrorState(errorMessage);
                }
            }
        }

        //private void setInfoOn(string infoMessage)
        //{
        //    if (InfoTooltip == null)
        //    {
        //        InfoTooltip = new ToolTip();
        //    }
        //    InfoTooltip.PlacementTarget = DataControl;
        //    InfoTooltip.Content = infoMessage;
        //    InfoTooltip.IsOpen = true;
        //}

        //private void setInfoOff()
        //{
        //    if (InfoTooltip != null)
        //    {
        //        InfoTooltip.IsOpen = false;
        //        //ErrorTooltip.PlacementTarget = null;
        //    }
        //}

        /// <summary>
        /// Gets or sets a value indicating whether this instance is full width.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is full width; otherwise, <c>false</c>.
        /// </value>
        public bool IsFullWidth 
        { 
            get
            {
                foreach (var child in this.Children)
                {
                    if (child is IFullWidth)
                    { return true; }
                }
                return false;
            } 
        }


        #region IDisposable Members

        public void Dispose()
        {
            this.Children.Clear();
        }

        #endregion
    }
}
